﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;

namespace DAO
{
    public class XuatXuDAO
    {
        // ----------- LOI ---------------
        public List<XuatXuDTO> LayDanhSachXuatXuL()
        {
            List<XuatXuDTO> danhsach = new List<XuatXuDTO>();
            string sql = "Select * From XUATXU";

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                XuatXuDTO xxDTO = new XuatXuDTO();
                xxDTO.MaXuatXu = reader.GetInt32(0);
                xxDTO.TenXuatXu = reader.GetString(1);
                danhsach.Add(xxDTO);
            }
            conn.Close();
            return danhsach;
        }
        public string LayTenXuatXuTheoMaL(int maXX)
        {
            string tenxx = "";
            string sql = "Select TenXuatXu From XUATXU Where MaXuatXu= " + maXX;

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                tenxx = reader.GetString(0);
            }
            conn.Close();
            return tenxx;
        }
        public int LayMaXuatXuTheoTenL(string ten)
        {
            int ma = 0;
            string sql = "Select MaXuatXu From XUATXU Where TenXuatXu= " + "N'" + ten + "'";

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ma = reader.GetInt32(0);
            }
            conn.Close();
            return ma;
        }
        //END ------------- LOI ----------------
        //---------------KIM----------------
        public int KLayMaXuatXuTheoTen(string ten)
        {
            int ma = 0;
            string sql = "Select MaXuatXu From XUATXU Where TenXuatXu= " + "N'" + ten + "'";

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ma = reader.GetInt32(0);
            }
            conn.Close();
            return ma;
        }
        public string KLayTenXuatXuTheoMa(int maXX)
        {
            string tenxx = "";
            string sql = "Select TenXuatXu From XUATXU Where MaXuatXu= " + maXX;

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                tenxx = reader.GetString(0);
            }
            conn.Close();
            return tenxx;
        }
        //END-------------------KIM------------------
        //---------------------HAI-------------------
        public List<XuatXuDTO> hai_LayDanhSachXuatXu()
        {
            List<XuatXuDTO> danhsach = new List<XuatXuDTO>();
            string sql = "Select * From XUATXU";

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                XuatXuDTO xxDTO = new XuatXuDTO();
                xxDTO.MaXuatXu = reader.GetInt32(0);
                xxDTO.TenXuatXu = reader.GetString(1);
                danhsach.Add(xxDTO);
            }
            conn.Close();
            return danhsach;
        }
        public string Hai_LayTenXuatXuTheoMa(int maXX)
        {
            string tenxx = "";
            string sql = "Select TenXuatXu From XUATXU Where MaXuatXu= " + maXX;

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                tenxx = reader.GetString(0);
            }
            conn.Close();
            return tenxx;
        }
        public int hai_LayMaXuatXuTheoTen(string ten)
        {
            int ma = 0;
            string sql = "Select MaXuatXu From XUATXU Where TenXuatXu= " + "N'" + ten + "'";

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ma = reader.GetInt32(0);
            }
            conn.Close();
            return ma;
        }
//-----------------------END HAI----------------------

    }
}
